package cn.edu.ctbu.sbadmin.lablog.webapi;

import cn.edu.ctbu.sbadmin.common.controller.BaseRestController;
import cn.edu.ctbu.sbadmin.common.utils.MQuery;
import cn.edu.ctbu.sbadmin.common.utils.MQueryHelper;
import cn.edu.ctbu.sbadmin.common.utils.PageUtils;
import cn.edu.ctbu.sbadmin.common.utils.R;
import cn.edu.ctbu.sbadmin.lablog.domain.LabLogInfoDO;
import cn.edu.ctbu.sbadmin.lablog.service.LabLogInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("webapi/lablog/labloginfo")
public class LabLogInfoRestController extends BaseRestController {


    @Autowired
    LabLogInfoService labLogInfoService;

    @RequestMapping("/getByPage")
    @RequiresPermissions("lablog:labloginfo:list")
    public PageUtils list(@RequestParam("where") String where,
                          @RequestParam("pageSize") Integer pageSize,
                          @RequestParam("pageIndex") Integer pageIndex,
                          @RequestParam("sort") String sort,
                          @RequestParam("direct") String direct
    ) throws Exception {
        //首先，把where条件的参数转为QueryAndItems
//        其中,where条件需要我们自己组装.示例:
//        name=张^:like&time=2^:l g&dmtCreate=2017-01-01^^2018-01-01
//        这个表示:
//        (name like '%张%' )and ( time<2 ) and (dmtCreate between '2017-01-01' and '2018-01-01)
        MQuery mQuery= MQueryHelper.GenQuery(where,pageSize,pageIndex,sort,direct,true);

        List<LabLogInfoDO> LabLogInfoDOList = labLogInfoService.listView(mQuery);

        int total=labLogInfoService.countView(mQuery);
        PageUtils pageUtils = new PageUtils(LabLogInfoDOList,total);

        return pageUtils;
    }

    @RequestMapping("/get")
    @RequiresPermissions("lablog:labloginfo:list")
    public R get(Long id) throws Exception{

        MQuery mQuery= MQueryHelper.GenQuery("dim_class.id="+id+"^:=",10,0,"","",true);
        List<LabLogInfoDO> LabLogInfoDOList=labLogInfoService.listView(mQuery);

        if(LabLogInfoDOList.size()>0)
            return R.ok("data",LabLogInfoDOList.get(0));

        return R.error(-1,"找不到数据");
    }

    /**
     * 保存数据
     * @param labLogInfoDO
     * @return
     */
    @RequestMapping("/save")
    @RequiresPermissions("lablog:labloginfo:add")
    public R save(LabLogInfoDO labLogInfoDO){
        if(labLogInfoService.save(labLogInfoDO)>0)
            return R.ok("data",labLogInfoDO);
        return R.error();

    }

    @RequestMapping("/update")
    @RequiresPermissions("lablog:labloginfo:edit")
    public R update(LabLogInfoDO labLogInfoDO){
        if(labLogInfoService.update(labLogInfoDO)>0){
            return R.ok("data",labLogInfoDO);
        }
        return R.error();
    }

    @RequestMapping("/batchDelete")
    @RequiresPermissions("lablog:labloginfo:remove")
    public R batchDelete(@RequestParam("ids[]") Long[] ids){
        List<LabLogInfoDO> labLogInfoDOS=labLogInfoService.findByIds(ids);
        if(labLogInfoDOS.size()>0){
            for(LabLogInfoDO labLogInfoDO:labLogInfoDOS){
                labLogInfoService.delete(labLogInfoDO.getId());
            }
        }
        return R.ok();
    }
}
